package com.zhcx.zhcxlibrary.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.zhcx.zhcxlibrary.R;
import com.zhcx.zhcxlibrary.utils.AnimationLoader;
import com.zhcx.zhcxlibrary.utils.DeviceUtils;
import com.zhcx.zhcxlibrary.utils.StringUtils;


/**
 * ProjectName: SmartBus
 * PackageName: com.zhcx.zhcxlibrary.widget
 * description:消息提示dialog
 * Date：2018/1/24 10:58
 * Author：Steven J
 * ClassName:PromptMessageDialog
 * Modify The Note:
 **/

public class PromptMessageDialog extends Dialog implements View.OnClickListener {
    private AnimationSet mAnimIn, mAnimOut;
    private View mBkgView, mDialogView;
    private TextView mTextTitle, mTextContent, mTextPositive;

    private OnPositiveListener mPositiveListener;
    private String mTitle;
    private String mContent;
    private String mPositive;
    private int mBackgroundColor;
    private boolean mIsShowAnim = false;

    public PromptMessageDialog(@NonNull Context context) {
        this(context, 0);
    }

    public PromptMessageDialog(@NonNull Context context, int themeResId) {
        super(context, R.style.color_dialog);
        initDialog();
    }

    private void initDialog() {
        mAnimIn = AnimationLoader.getInAnimation(getContext());
        mAnimOut = AnimationLoader.getOutAnimation(getContext());
        initAnimListener();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View contentView = View.inflate(getContext(), R.layout.layout_promptmessage_dialog, null);
        setContentView(contentView);
        mDialogView = getWindow().getDecorView().findViewById(android.R.id.content);
        mBkgView = contentView.findViewById(R.id.llBkg);
        mTextTitle = (TextView) contentView.findViewById(R.id.text_title);
        mTextContent = (TextView) contentView.findViewById(R.id.text_content);
        mTextPositive = (TextView) contentView.findViewById(R.id.text_positive);
        mTextPositive.setOnClickListener(this);
        if (!StringUtils.isEmpty(mTitle)) {
            mTextTitle.setText(mTitle);
        }
        if (!StringUtils.isEmpty(mContent)) {
            mTextContent.setText(mContent);
        }
        if (!StringUtils.isEmpty(mPositive)) {
            mTextPositive.setText(mPositive);
        }
        setBackgroundColor();
    }

    private void initAnimListener() {
        mAnimOut.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mDialogView.post(new Runnable() {
                    @Override
                    public void run() {
                        callDismiss();
                    }
                });
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
    }

    private void callDismiss() {
        super.dismiss();
    }

    private void setBackgroundColor() {
        if (0 == mBackgroundColor) {
            return;
        }
        int radius = DeviceUtils.dip2px(getContext(), 6);
        float[] outerRadii = new float[]{radius, radius, radius, radius, 0, 0, 0, 0};
        RoundRectShape roundRectShape = new RoundRectShape(outerRadii, null, null);
        ShapeDrawable shapeDrawable = new ShapeDrawable(roundRectShape);
        shapeDrawable.getPaint().setColor(mBackgroundColor);
        shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
        mBkgView.setBackgroundDrawable(shapeDrawable);
    }

    public PromptMessageDialog setColor(int color) {
        mBackgroundColor = color;
        return this;
    }

    public PromptMessageDialog setPositiveListener(String text, OnPositiveListener positiveListener) {
        mPositive = text;
        mPositiveListener = positiveListener;
        return this;
    }

    public PromptMessageDialog setPositiveListener(int textId, OnPositiveListener l) {
        return setPositiveListener(getContext().getText(textId).toString(), l);
    }

    @Override
    protected void onStart() {
        super.onStart();
        startWithAnimation(mIsShowAnim);
    }

    @Override
    public void dismiss() {
        dismissWithAnimation(mIsShowAnim);
    }

    private void dismissWithAnimation(boolean showOutAnimation) {
        if (showOutAnimation) {
            mDialogView.startAnimation(mAnimOut);
        } else {
            super.dismiss();
        }
    }

    private void startWithAnimation(boolean showInAnimation) {
        if (showInAnimation) {
            mDialogView.startAnimation(mAnimIn);
        }
    }

    public PromptMessageDialog setAnimationEnable(boolean enable) {
        mIsShowAnim = enable;
        return this;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (R.id.text_positive == id) {
            LogUtils.d("linear_positive");
            mPositiveListener.onClick(this);
        }
    }

    public PromptMessageDialog setDialogTitle(String title) {
        mTitle = title;
        return this;
    }

    public PromptMessageDialog setDialogTitle(int title) {
        return setDialogTitle(getContext().getText(title).toString());
    }

    public PromptMessageDialog setDialogContent(String content) {
        if (!StringUtils.isEmpty(content)) {
            mContent = content;
        }
        return this;
    }

    public PromptMessageDialog setDialogContent(int title) {
        return setDialogContent(getContext().getText(title).toString());
    }

    public interface OnPositiveListener {
        void onClick(PromptMessageDialog dialog);
    }
}
